package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.exception.LeadException;
import com.heima.common.util.JwtUtils;
import com.heima.common.util.MD5Utils;
import com.heima.model.media.dtos.WmUserDto;
import com.heima.model.media.dtos.WmUserLoginDto;
import com.heima.wemedia.entity.WmUser;
import com.heima.wemedia.mapper.WmUserMapper;
import com.heima.wemedia.service.WmUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class WmUserServiceImpl extends ServiceImpl<WmUserMapper, WmUser> implements WmUserService {

    @Autowired
    private BCryptPasswordEncoder encoder;

    /**
     * 自媒体用户登录
     *
     * @param wmUserLoginDto
     * @return
     */
    @Override
    public Map<String, Object> login(WmUserLoginDto wmUserLoginDto) {
//        构造查询条件
        QueryWrapper<WmUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(WmUser::getName, wmUserLoginDto.getName());
        WmUser wmUser = this.getOne(queryWrapper);
        if (wmUser == null) {
            throw new LeadException(AppHttpCodeEnum.DATA_NOT_EXIST, "用户不存在！");
        }
        boolean b = encoder.matches(wmUserLoginDto.getPassword(), wmUser.getPassword());
        if (!b) {
            throw new LeadException(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
        }
//        生成token
        String token = JwtUtils.generateTokenExpireInSeconds(wmUser.getId(), 3600);
        Map<String, Object> map = new HashMap<>();
        wmUser.setPassword("");
        map.put("user", wmUser);
        map.put("token", token);
        return map;
    }

    //自媒体管理--保存自媒体用户

    /**
     * 保存自媒体用户
     *
     * @param wmUserDto
     * @return
     */
    @Override
    public Integer saveWmUser(WmUserDto dto) {
        Integer apUserId = dto.getApUserId();
        LambdaQueryWrapper<WmUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmUser::getApUserId, apUserId);
        WmUser wmUser = getOne(queryWrapper);
        if (wmUser == null) {
            wmUser = new WmUser();
            wmUser.setApUserId(apUserId);
            wmUser.setStatus(9);
            wmUser.setCreatedTime(new Date());
            wmUser.setType(0);
        }
        wmUser.setName(dto.getName());
        wmUser.setPassword(dto.getPassword());
        wmUser.setPhone(dto.getPhone());
        boolean b = saveOrUpdate(wmUser);
        if (!b) {
            log.error("新增或修改自媒体用户失败！");
            throw new LeadException(AppHttpCodeEnum.SERVER_ERROR);
        }
        return wmUser.getId();
    }
}